﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.Objects;
using System.Data.Objects.DataClasses;
namespace school
{
    public partial class CourseViewer : Form
    {
        private SchoolEntities schoolContext;
        public CourseViewer()
        {
            InitializeComponent();
        }

        private void closeForm_Click(object sender, EventArgs e)
        {
            schoolContext.Dispose();
            this.Close();
        }

        private void CourseViewer_Load(object sender, EventArgs e)
        {
            //Initialize the ObjectContext
            schoolContext = new SchoolEntities();

            // Define a query that returns all Department  
            // objects and course objects, ordered by name.
            var departmentQuery = from d in schoolContext.Departments.Include("Courses")
                                  orderby d.Name
                                  select d;
            try
            {
                // Bind the ComboBox control to the query, 
                // which is executed during data binding.
                // To prevent the query from being executed multiple times during binding, 
                // it is recommended to bind controls to the result of the Execute method. 
                this.departmentList.DisplayMember = "Name";
                this.departmentList.DataSource = ((ObjectQuery)departmentQuery).Execute(MergeOption.AppendOnly);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void departmentList_SelectedIndexChanged(object sender, EventArgs e)
        {
            try
            {
                //Get the object for the selected department.
                Department department = (Department)this.departmentList.SelectedItem;

                //Bind the grid view to the collection of Course objects
                // that are related to the selected Department object.
                courseGridView.DataSource = department.Courses;

                // Hide the columns that are bound to the navigation properties on Course.
                courseGridView.Columns["Department"].Visible = false;
                courseGridView.Columns["StudentGrades"].Visible = false;
                courseGridView.Columns["OnlineCourse"].Visible = false;
                courseGridView.Columns["OnsiteCourse"].Visible = false;
                courseGridView.Columns["People"].Visible = false;
                courseGridView.Columns["DepartmentId"].Visible = false;

                courseGridView.AllowUserToDeleteRows = false;
                courseGridView.AutoResizeColumns(DataGridViewAutoSizeColumnsMode.AllCells);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void saveChanges_Click(object sender, EventArgs e)
        {
            try
            {

                // Save object changes to the database, 
                // display a message, and refresh the form.
               
                schoolContext.SaveChanges();
                MessageBox.Show("Changes saved to the database.");
                this.Refresh();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
        int i = 53;
        private void btnInsert_Click(object sender, EventArgs e)
        {
            Department departement;
            departement = new Department();

            DateTime ma_date = DateTime.Now;//DAT_DATE.Value;

            departement.DepartmentID = i;
            departement.Administrator =1 ;//Int32.Parse(TEX_ADMINISTRATEUR.Text);
            departement.Budget = 2;//Decimal.Parse(TEX_BUDGET.Text);
            departement.Name ="ssss" ;//TEX_NAME.Text;
            departement.StartDate = ma_date;

            schoolContext.AddToDepartments(departement);

            try
            {
                // Save object changes to the database, 
                // display a message, and refresh the form.
                schoolContext.SaveChanges();
                i++;
                MessageBox.Show("Creation reussie");
                this.Refresh();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }

            MessageBox.Show(departement.Administrator + "   -   " + departement.Budget + "    -    " + departement.Name);

            
        }

        private void btnUpdate_Click(object sender, EventArgs e)
        {
            Department departement;
            departement = new Department();

            var tous_les_departements = from d in schoolContext.Departments
                                        select d;


            foreach (Department departement_en_cours in tous_les_departements)
            {
                departement_en_cours.Budget = decimal.Parse("1511");
            }

            try
            {
                // Save object changes to the database, 
                // display a message, and refresh the form.

                // Mark the customer as updated.

                schoolContext.SaveChanges();
                MessageBox.Show("Modification reussie");
                this.Refresh();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void btnDelete_Click(object sender, EventArgs e)
        {
           // Course course = new Course();
            //if (courseGridView.SelectedRows.Count > 0)
            //{

            //    int CourseID = Convert.ToInt32(courseGridView.SelectedRows[0].Cells[0].Value.ToString());
            //    var tableCourseonline = from d in schoolContext.OnlineCourses
            //                      where d.CourseID == CourseID
            //                      select d;

            //    foreach (OnlineCourse item in tableCourseonline)
            //    {
            //        MessageBox.Show(item.URL);
            //        schoolContext.DeleteObject(item);
            //    }
            //    //MessageBox.Show(courseGridView.SelectedRows[0].Cells[0].Value.ToString());
            //    try
            //    {
            //        // Save object changes to the database, 
            //        // display a message, and refresh the form.

            //        // Mark the customer as updated.

            //        schoolContext.SaveChanges();
            //        MessageBox.Show("Suppression reussie");
            //        this.Refresh();
            //    }
            //    catch (Exception ex)
            //    {
            //        MessageBox.Show(ex.InnerException.ToString());
            //    }
            //}
            //else
            //{
            //    MessageBox.Show("Chua chon item");
            //}
            if (courseGridView.SelectedRows.Count > 0)
            {

                int CourseID = Convert.ToInt32(courseGridView.SelectedRows[0].Cells[0].Value.ToString());
                var tableCourse = from d in schoolContext.Courses
                                        where d.CourseID == CourseID
                                        select d;

                foreach (Course item in tableCourse)
                {
                    MessageBox.Show(item.Title);
                    schoolContext.DeleteObject(item);
                }
                //MessageBox.Show(courseGridView.SelectedRows[0].Cells[0].Value.ToString());
                try
                {
                    // Save object changes to the database, 
                    // display a message, and refresh the form.

                    // Mark the customer as updated.

                    schoolContext.SaveChanges();
                    MessageBox.Show("Suppression reussie");
                    this.Refresh();
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.InnerException.ToString());
                }
            }
            else
            {
                MessageBox.Show("Chua chon item");
            }
            
        }
     

        
    }
}
